import { getStream } from '../Core/connect'
import { Material } from '../Material/Material'
import { Object3D } from './Object3D'
import * as MathUtils from '../Math/MathUtils'
import { Vector3 } from '../Math/Vector3'


export class Polygon extends Object3D {
    readonly type: string
    height: number
    constructor(name: string, height: number = 1000) {
        super({name: name, type: 'Polygon'})
        this.type = 'Polygon'
        this.height = height   
            this.setHight(height)
            this.setPlace()    
    }
    setHight(height: number) {
        this.height = height
        const stream = getStream()
        const str = {function: 'setPolygonHeight', ids: `${this.uuid}`, height:`${this.height}`}
        stream.emitUIInteraction(str)
    }
    setMaterial(material: Material) {
        const stream = getStream()
        const str = {function: 'setPolygonColor', ids: `${this.uuid}`, Material:material}
        stream.emitUIInteraction(str)
    }
    
    setPlace() {
        const stream = getStream()
        const str = {function: 'setPolygonPlace', ids: `${this.uuid}`, name:`${this.name}`}
        stream.emitUIInteraction(str)
    }
    setPolygonByUrl(url: string, Height: number) {
        const stream = getStream()
        const str = {function: 'setPolygonByUrl',  ids: `${this.uuid}`, url:`${url}` ,height:`${Height}`}
        stream.emitUIInteraction(str)
    }
    buildByList(list:Array<Vector3>, Height: number) {
        const stream = getStream()
        const str = {function: 'buildPolygonByList', ids: `${this.uuid}`, pointList: list, height: Height}
        stream.emitUIInteraction(str)
    }

    build() {
        const stream = getStream()
        const str = {function: 'buildPolygon', ids: `${this.uuid}`}
        stream.emitUIInteraction(str)
    }
    setClick(fun: Function) {
        const stream = getStream()
        const uuid = this.uuid
        const sessionId = MathUtils.generateUUID()
        const str = {function: 'setPolygonClick', ids: `${this.uuid}`, sessionId: sessionId}
        stream.emitUIInteraction(str)
        const getSpriteClick = (data: string) => {
            const returnData = JSON.parse(data)
            if (returnData.uuid === uuid) {
                fun()
            }
        }
        stream.addResponseEventListener(`polygonClick${this.uuid}`, getSpriteClick)
    }

}

